package verticle;

import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.http.HttpVersion;
import io.vertx.reactivex.core.AbstractVerticle;
import io.vertx.reactivex.core.http.HttpClient;
import io.vertx.reactivex.core.http.HttpClientRequest;

/**
 * FrontEndVerticle
 *
 * @author liyixin
 * @date 2019/1/24
 */
public class FrontEndVerticle extends AbstractVerticle {

    @Override
    public void start() {
        HttpClientOptions clientOptions = new HttpClientOptions()
                .setProtocolVersion(HttpVersion.HTTP_2)
                .setHttp2ClearTextUpgrade(false)
                .setHttp2MaxPoolSize(100).setKeepAlive(true);

        HttpClient client = vertx.createHttpClient(clientOptions);

        vertx.createHttpServer(new HttpServerOptions()).requestHandler(req -> {
            final String method = req.rawMethod();
            if("GET".equals(method)) {
                req.response().end("test");
            } else if("PUT".equals(method)) {
                HttpClientRequest put = client.put(90, Config.ip,"/");
                put.toFlowable().subscribe(res -> res.handler(v -> {}).endHandler(v -> req.response().end()));
                put.headers().add("Content-Length",req.getHeader("content-length"));
                req.handler(put::write).endHandler(v -> {
                    put.end();
                    System.out.println(put.connection() == null);
                });
            }
        }).listen(80);
    }
}
